<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%--<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">--%>
    <script src="js/jquery-3.4.1.min.js"></script>
</head>
<body>
<form action="welcome" method="post">
    <input name="username"/>
    <button type="submit">Go</button>
</form>
<button onclick="ajaxDemo();">ajax</button>
<br/>
图片上传：<input type="file" name="file1" multiple="multiple" id="file0" />
<img src="" id="img0">
<button id="ajax_upload">Ajax上传</button>
<script>
    $(function () {
        $("#file0").change(function () {
            var formData = new FormData();
            formData.append("file",document.getElementById("file0").files[0]);
            $.ajax({
                url : "ajaxUpload",
                data : formData,
                type : "post",
                contentType:false,
                processData:false,
                dataType : "json",
                success : function(response){
                    if(response.dataState == "success"){
                        // 为 src 属性设置上传文件的相对路径，实现文件预览
                        $("#img0").attr("src",response.responseMap.fileUrl);
                        console.log(response.responseMap.fileUrl);
                    }else{
                        alert(response.dataState);
                    }
                }
            });
        });


        $("#ajax_upload").click(function () {
            var formData = new FormData();
            formData.append("file",document.getElementById("file0").files[0]);
            $.ajax({
                url : "ajaxUpload",
                data : formData,
                type : "post",
                contentType:false,
                processData:false,
                dataType : "json",
                success : function(response){
                    console.log(response);
                }
            });
        });
    });


    // function getObjectUrl(file){
    //     var url = null;
    //     if(window.URL)
    // }
    function ajaxDemo(){
        $.ajax({
            url : "ajaxDemo",
            data : {"username":"赵妹儿"},
            type : "post",
            // contentType : "text/plain",
            dataType : "text",
            success : function (data) {
                console.log(data);
                console.log(1);
            },
            error : function (e) {
                console.log(e);
            }
        });
    }
</script>
</body>
</html>
